package com.yfp.tissue.service.impl;

import com.github.pagehelper.util.StringUtil;
import com.yfp.common.core.domain.R;
import com.yfp.common.core.utils.uuid.Snowflake;
import com.yfp.common.security.utils.SecurityUtils;
import com.yfp.system.api.domain.SysDictData;
import com.yfp.system.api.domain.SysUser;
import com.yfp.system.api.model.LoginUser;
import com.yfp.tissue.domain.Dto.CompanyDto;
import com.yfp.tissue.domain.Dto.CompanyFindListDTO;
import com.yfp.tissue.domain.entity.AlipayInfo;
import com.yfp.tissue.domain.entity.Company;
import com.yfp.tissue.domain.entity.WxpayInfo;
import com.yfp.tissue.domain.vo.CompanyVO;
import com.yfp.tissue.mapper.*;
import com.yfp.tissue.mapper.*;
import com.yfp.tissue.service.CompanySerivce;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;

import static com.yfp.common.core.utils.PageUtils.startPage;

/**
 * @PROJECT_NAME: ydyfp
 * @DESCRIPTION:
 * @USER: xiaohuya
 * @DATE: 2023/12/14 16:14
 */

@Service
@Slf4j

public class CompanyServiceImpl implements CompanySerivce {
    @Resource
    private CompanyMapper companyMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Resource
    private AlipayInfoMapper alipayInfoMapper;
    @Resource
    private WxpayInfoMapper wxpayInfoMapper;
    @Resource
    private SysUserCompanyMapper sysUserCompanyMapper;

    private static final Pattern PHONE_PATTERN = Pattern.compile("^1[3-9]\\d{9}$");

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R addCompany(CompanyDto companyDto) {
        Snowflake snowflake = new Snowflake(1);
        companyDto.setCompanyId(String.valueOf(snowflake.nextId()));
        SysUser sysUser = userMapper.selectUserById(SecurityUtils.getUserId());

        SysUser users = userMapper.selectUserByUserName(companyDto.getCompanyAccount());
        //校验公司账号是否符合手机号规范
        if (!isValid(companyDto.getCompanyAccount())) {
            return R.fail("公司账号不符合手机号规范");
        }
        //校验公司账号是否存在
        if (users != null) {
            return R.fail("公司账号已经存在");
        }
        //校验联系方式是否符合手机号规范
        if (!isValid(companyDto.getMobile())) {
            return R.fail("联系方式不符合手机号规范");
        }
        SysUser user = new SysUser();
        user.setPassword(SecurityUtils.encryptPassword("888888"));
        user.setUserName(companyDto.getCompanyAccount());
        user.setNickName(companyDto.getName());
        user.setPhonenumber(companyDto.getCompanyAccount());
        user.setRoleId(2L);
        user.setStatus("0");
        String companyId = String.valueOf(snowflake.nextId());
        user.setUserName(companyDto.getCompanyAccount());
        user.setNickName(companyDto.getName());
        user.setCompanyId(companyId);
        user.setCreateBy(sysUser.getNickName());
        userMapper.insertUser(user);

        //实体类dto转换
//        Company company = entityMapper.companyToDto(companyDto);
        companyDto.setCompanyId(companyId);
        Company company = new Company();
//        BeanUtils.copyProperties(companyDto,company);
        BeanUtils.copyProperties(companyDto, company);
        LoginUser loginUser = SecurityUtils.getLoginUser();

        company.setUpdateName(loginUser.getSysUser().getNickName());
        company.setGroupMeal(0);
        companyMapper.insert(company);

//        WxpayInfo wxpayInfo = entityMapper.wxpayToDto(companyDto);
        WxpayInfo wxpayInfo = new WxpayInfo();
        BeanUtils.copyProperties(companyDto, wxpayInfo);
        wxpayInfo.setCompanyId(companyId);
        wxpayInfoMapper.insert(wxpayInfo);

//        AlipayInfo alipayInfo = entityMapper.alipayToDto(companyDto);
        AlipayInfo alipayInfo = new AlipayInfo();
        BeanUtils.copyProperties(companyDto, alipayInfo);
        alipayInfoMapper.insert(alipayInfo);

        return R.ok();
    }

    @Override
    public List<CompanyVO> queryCompany(CompanyDto companyDto) {
        startPage();
        if (companyDto.getBizLicenceEndTime()==null&&companyDto.getBizLicenceBeginTime()!=null){
            companyDto.setLongTerm("1");
        }
        List<CompanyVO> companyVOS = companyMapper.queryAllByLimit(companyDto);
        return companyVOS;
    }

    @Override
    public List<CompanyVO> findList(String cityName) {
        CompanyFindListDTO companyFindListDTO = new CompanyFindListDTO();
        if (!StringUtil.isEmpty(cityName)) {
            companyFindListDTO.setCityName(cityName);
        }
        return companyMapper.findList(companyFindListDTO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateCompany(CompanyDto companyDto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        companyDto.setUpdateName(loginUser.getSysUser().getNickName());
        companyMapper.update(companyDto);
        alipayInfoMapper.update(companyDto);
        wxpayInfoMapper.update(companyDto);
        return R.ok();
    }

    @Override
    public R queryParen(String companyId) {
        List<CompanyVO> companyVOS = new ArrayList<>();
        if (companyId.equals("15271848757")) {

            companyVOS = companyMapper.queryNameList(null);
        } else {
            companyVOS = companyMapper.queryNameList(companyId);

        }

        List<SysDictData> sysPayment = companyMapper.selectDict("sys_payment");
        List<SysDictData> cooperation = companyMapper.selectDict("sys_cooperation");
        HashMap<String, List> parenMapper = new HashMap<>();
        parenMapper.put("companys", companyVOS);
        parenMapper.put("sysPayment", sysPayment);
        parenMapper.put("cooperation", cooperation);
        return R.ok(parenMapper);
    }

    @Override
    public R queryOneCompany(String companyId) {
        CompanyVO companyVO = companyMapper.oneNameList(companyId);
        AlipayInfo alipayInfo = alipayInfoMapper.queryById(companyId);
        WxpayInfo wxpayInfo = wxpayInfoMapper.queryById(companyId);
        HashMap<String, Object> parenMapper = new HashMap<>();
        parenMapper.put("companys", companyVO);
        parenMapper.put("alipayInfo", alipayInfo);
        parenMapper.put("wxpayInfo", wxpayInfo);

        return R.ok(parenMapper);
    }

    @Override
    public void add(Company company) {
        companyMapper.add(company);
    }

    @Override
    public Company findByCompanyAccount(String companyAccount) {
        return companyMapper.findByCompanyAccount(companyAccount);
    }

    @Override
    public void del(String id) {
        companyMapper.del(id);
    }

    public static boolean isValid(String phone) {
        if (phone == null) {
            return false;
        }
        return PHONE_PATTERN.matcher(phone).matches();
    }

    public static void main(String[] args) {

        System.out.println(SecurityUtils.matchesPassword("888888", "$2a$10$fTi.ns25rsC.BjzH2ViBauTcHIvTWBD8ttScC8kQiM47JfNzkr/5O"));
    }
}
